//
//  TYZRadioGroup.m
//  VehicleInsuranceSupport
//
//  Created by TangBin on 2019/11/25.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZRadioGroup.h"

@implementation TYZRadioGroup
- (void)dealloc
{
//    debugMethod();
}

- (id)init
{
    if (self=[super init])
    {
        _children = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)add:(TYZCheckButton *)cb
{
    cb.delegate = self;
    if (cb.checked)
    {
        _text = cb.label.text;
        _value = cb.value;
    }
    [self.children addObject:cb];
}

- (void)hidden:(BOOL)hidden
{
    for (TYZCheckButton *btn in self.children)
    {
        btn.hidden = hidden;
    }
}

- (void)enableTouch:(BOOL)enabled
{
    for (TYZCheckButton *btn in self.children)
    {
        btn.enabled = enabled;
    }
}

- (void)selectChecked:(int)arindex
{
    for (int i=0; i<[self.children count]; i++)
    {
        TYZCheckButton *each = [self.children objectAtIndex:i];
        
        if (i== arindex)
        {
            [each setChecked:YES];
        }
        else
        {
            [each setChecked:NO];
        }
    }
    
}

- (void)checkButtonClicked:(id)sender
{
    NSLog(@"%s", __func__);
    TYZCheckButton *cb = (TYZCheckButton *)sender;
    if (!cb.checked)
    {
        //        实现单选
        for (TYZCheckButton *each in self.children)
        {
            if (each.checked)
            {
                [each setChecked:NO];
            }
        }
        cb.isClicked = YES;
        [cb setChecked:YES];
        //        复制选择的项
        _text = cb.label.text;
        _value = cb.value;
    }
    
    if (_delegate != nil)
    {
        SEL sel = NSSelectorFromString(@"selectChanged:");
        if ([_delegate respondsToSelector:sel])
        {
            [_delegate performSelector:sel withObject:sender];
        }
    }
}

// selectdValue
- (void)selectdValue:(NSDictionary *)dict
{
    NSString *s = dict[@"text"];
    id o = dict[@"index"];
    _text = s;
    _value = o;
    if ([_rgDelegate respondsToSelector:@selector(selectedText:index:)])
    {
        [_rgDelegate selectedText:s index:[o integerValue]];
    }
}
@end
